#include <stdio.h>
#include <string.h>

#include "Types.h"
#include "app_xml.h"

static char xml[] = "\
<?xml version=\"1.0\" encoding=\"utf-8\" ?>\
<root>\
<AAA>344</AAA>\
<part id = \"01\" option=\"abbb\">\
<name>张三</name>\
<age>20</age>\
<sex>男</sex>\
<like><![CDATA[可以看到吗?]]></like>\
</part>\
<listTest>\
<test>\
<x>1</x>\
</test>\
<test>\
<x>2</x>\
</test>\
</listTest>\
<arrList>\
<arr>\
<xxx>abc</xxx>\
<yyy>1</yyy>\
</arr>\
<arr>\
<xxx>cde</xxx>\
<yyy>2</yyy>\
</arr>\
</arrList>\
</root>\
";

int main()
{
    XML_NODE_s *pstXml = NULL;
    XML_NODE_s *rootNode = NULL;
    XML_NODE_s *listTestNode = NULL;
    XML_NODE_s *testNode = NULL;
    XML_NODE_s *Node = NULL;
    XML_NODE_s *partNode = NULL;
    XML_NODE_s *arrListNode = NULL;
    XML_NODE_s *arrNode = NULL;
    ULONG uRet = STAT_SUCCEED;
    char szValue[512] = { 0 };
    int iValue = 0;

    //解析
    uRet = xml_loadString(xml, &pstXml);
    if (uRet != STAT_SUCCEED)
    {
        printf("[%s %d] xml_loadString fail, 0x%lx\n", __FUNCTION__, __LINE__, uRet);
        return -1;
    }

    uRet = xml_saveFile(pstXml, "/tmp/Save.xml");
    if (uRet != STAT_SUCCEED)
    {
        printf("[%s %d] xml_saveFile fail, 0x%lx\n", __FUNCTION__, __LINE__, uRet);
        return -2;
    }
    
    xml_destory(&pstXml);
    
    uRet = xml_loadFile("/tmp/SaveChg.xml", &pstXml);
    if (uRet != STAT_SUCCEED)
    {
        printf("[%s %d] xml_loadFile fail, 0x%lx\n", __FUNCTION__, __LINE__, uRet);
        return -3;
    }
    
    uRet = xml_getChildByName(pstXml, "root", &rootNode);
    if (uRet != STAT_SUCCEED)
    {
        printf("[%s %d] xml_getChildByName fail, 0x%lx\n", __FUNCTION__, __LINE__, uRet);
        return -4;
    }
    
    //获取属性
    uRet = xml_getChildByName(rootNode, "part", &partNode);
    if (uRet != STAT_SUCCEED)
    {
        printf("[%s %d] xml_getChildByName fail, 0x%lx\n", __FUNCTION__, __LINE__, uRet);
        return -5;
    }

    uRet = xml_getNodeAttrStr(partNode, "id", sizeof(szValue), szValue);
    if (uRet != STAT_SUCCEED)
    {
        printf("[%s %d] xml_getNodeAttrStr fail, 0x%lx\n", __FUNCTION__, __LINE__, uRet);
        return -6;
    }
    
    uRet = xml_setNodeAttrStr(partNode, "id", "08");
    if (uRet != STAT_SUCCEED)
    {
        printf("[%s %d] xml_setNodeAttrStr fail, 0x%lx\n", __FUNCTION__, __LINE__, uRet);
        return -7;
    }
    
    printf("[%s %d] attr is [%s]\n", __FUNCTION__, __LINE__, szValue);
    
    uRet = xml_getNodeByPath(pstXml, "root/part/name", &testNode);
    if (uRet != STAT_SUCCEED)
    {
        printf("[%s %d] xml_getNodeByPath fail, 0x%lx\n", __FUNCTION__, __LINE__, uRet);
        return -8;
    }
    
    uRet = xml_getNodeValueStr(testNode, sizeof(szValue), szValue);
    if (uRet != STAT_SUCCEED)
    {
        printf("[%s %d] xml_getNodeValueStr fail, 0x%lx\n", __FUNCTION__, __LINE__, uRet);
        return -9;
    }
    
    printf("[%s %d] Value is [%s]\n", __FUNCTION__, __LINE__, szValue);
    
    uRet = xml_setNodeValueStr(testNode, "李四");
    if (uRet != STAT_SUCCEED)
    {
        printf("[%s %d] xml_setNodeValueStr fail, 0x%lx\n", __FUNCTION__, __LINE__, uRet);
        return -10;
    }

    uRet = xml_getNodeByPath(pstXml, "root/part/like", &testNode);
    if (uRet != STAT_SUCCEED)
    {
        printf("[%s %d] xml_getNodeByPath fail, 0x%lx\n", __FUNCTION__, __LINE__, uRet);
        return -11;
    }
    
    uRet = xml_getNodeValueCData(testNode, 0, sizeof(szValue), szValue);
    if (uRet != STAT_SUCCEED)
    {
        printf("[%s %d] xml_getNodeValueCData fail, 0x%lx\n", __FUNCTION__, __LINE__, uRet);
        return -12;
    }
    
    printf("[%s %d] CDATA is [%s]\n", __FUNCTION__, __LINE__, szValue);
    
    uRet = xml_setNodeValueCData(testNode, 0, "我逗逗你玩！");
    if (uRet != STAT_SUCCEED)
    {
        printf("[%s %d] xml_setNodeValueCData fail, 0x%lx\n", __FUNCTION__, __LINE__, uRet);
        return -13;
    }
    
    uRet = xml_getSonNodeValueCData(rootNode, "like", sizeof(szValue), szValue);
    if (uRet != STAT_SUCCEED)
    {
        printf("[%s %d] xml_getSonNodeValueCData fail, 0x%lx\n", __FUNCTION__, __LINE__, uRet);
        return -14;
    }
    
    printf("[%s %d] CDATA is [%s]\n", __FUNCTION__, __LINE__, szValue);
    
    //解析listTest
    uRet = xml_getChildByName(rootNode, "listTest", &listTestNode);
    if (uRet != STAT_SUCCEED)
    {
        printf("[%s %d] xml_getChildByName fail, 0x%lx\n", __FUNCTION__, __LINE__, uRet);
        return -15;
    }
    
    xml_getFirstChildByName(listTestNode, "test", &testNode);
	
    while (testNode)
    {
        uRet = xml_getChildByName(testNode, "x", &Node);
        if (uRet != STAT_SUCCEED)
        {
            break;
        }

        xml_getNodeValueInt(Node, &iValue);
        printf("[%s %d] getNodeValueInt is [%d]\n", __FUNCTION__, __LINE__, iValue);

        xml_getNextSiblingByName(testNode, "test", &testNode);
    }
    
    //解析arrList
    uRet = xml_getFirstChildByName(rootNode, "arrList", &arrListNode);
    if (uRet != STAT_SUCCEED)
    {
        printf("[%s %d] xml_getFirstChildByName fail, 0x%lx\n", __FUNCTION__, __LINE__, uRet);
        return -16;
    }
    
    xml_getFirstChildByName(arrListNode, "arr", &arrNode);
 
    while (arrNode)
    {
        memset(szValue, 0, 100);
        xml_getSonNodeValueInt(arrNode, "yyy", &iValue);
        printf("[%s %d] yyy is %d\n", __FUNCTION__, __LINE__, iValue);
 
        xml_getSonNodeValueStr(arrNode, "xxx", sizeof(szValue), szValue);
        printf("[%s %d] xxx is %s\n", __FUNCTION__, __LINE__, szValue);

        xml_getNextSiblingByName(arrNode, "arr", &arrNode);
    }

    uRet = xml_addNodeStr(pstXml, "root/part", "NewNode", "new code");
    if (uRet != STAT_SUCCEED)
    {
        printf("[%s %d] xml_addNodeStr fail, 0x%lx\n", __FUNCTION__, __LINE__, uRet);
        return -17;
    }

    uRet = xml_addNodeCDATA(pstXml, "root/part", "NewCDATA", "new cdata");
    if (uRet != STAT_SUCCEED)
    {
        printf("[%s %d] xml_addNodeCDATA fail, 0x%lx\n", __FUNCTION__, __LINE__, uRet);
        return -18;
    }
 
    uRet = xml_saveFile(pstXml, "/tmp/SaveChg.xml");
    if (uRet != STAT_SUCCEED)
    {
        printf("[%s %d] xml_saveFile fail, 0x%lx\n", __FUNCTION__, __LINE__, uRet);
        return -19;
    }
 
    xml_destory(&pstXml);
 
    return 0;
}
